<template>
  <div class="content">
    <el-row :gutter="20" class="width-100">
      <el-col :span="18" :offset="3">
        <el-card shadow="never" class=" border-radius-10">
          <h1 style="text-align: center;font-size: 20px;padding-bottom: 10px;">企业认证</h1>
          <div class="grid-content ep-bg-purple width-100">
            <el-form :model="form" ref="formRef" :rules="rules" label-width="160px" :inline="false">
              <el-form-item label="姓名" prop="first_name">
                <el-input v-model="form.first_name" placeholder="姓名"></el-input>
              </el-form-item>
              <el-form-item label="手机号" prop="mobile">
                <el-input v-model="form.mobile" placeholder="手机号"></el-input>
              </el-form-item>
              <el-form-item label="昵称" prop="nickname">
                <el-input v-model="form.nickname" placeholder="昵称"></el-input>
              </el-form-item>

              <el-form-item label="身份证号" prop="id_card">
                <el-input v-model="form.id_card" placeholder="身份证号"></el-input>
              </el-form-item>

              <el-form-item label="上传身份证头像面照片">
                <el-col :span="12">
                  <UploadFile style="width: 200px;"
                  :data="{ img_url: form.id_card_front_url, image_show_url: image_id_card_front_url }"
                  @success="handleCardFrontUploadSuccess" />
                </el-col>
                <el-col :span="12">
                  <UploadFile style="width: 200px;"
                  :data="{ img_url: form.id_card_back_url, image_show_url: image_id_card_back_url }"
                  @success="handleCardBackUploadSuccess" />
                </el-col>
              </el-form-item>

              <el-form-item label="公司名称" prop="name">
                <el-input v-model="store.state.user.company_name" disabled placeholder="公司名称"></el-input>
              </el-form-item>

              <el-form-item label="营业执照编号" prop="company_license_number">
                <el-input v-model="form.company_license_number" placeholder="营业执照编号"></el-input>
              </el-form-item>

              <el-form-item label="法人手机号" prop="company_mobile">
                <el-input v-model="form.company_mobile" placeholder="法人手机号"></el-input>
              </el-form-item>

              <el-form-item label="法人身份证号" prop="company_id_card">
                <el-input v-model="form.company_id_card" placeholder="法人身份证号"></el-input>
              </el-form-item>

              <el-form-item label="上传法人身份证头像面照片">
                <el-col :span="12">
                  <UploadFile style="width: 200px;"
                    :data="{ img_url: form.company_corporation_front_url, image_show_url: image_company_corporation_front_url }"
                    @success="handleCorporationCardFrontUploadSuccess" />
                </el-col>
                <el-col :span="12">
                  <UploadFile style="width: 200px;"
                    :data="{ img_url: form.company_corporation_back_url, image_show_url: image_company_corporation_back_url }"
                    @success="handleCorporationCardBackUploadSuccess" />
                </el-col>

              </el-form-item>


              <el-form-item label="法人姓名" prop="company_corporation">
                <el-input v-model="form.company_corporation" placeholder="法人姓名"></el-input>
              </el-form-item>

              <el-form-item label="上传营业执照">

                <el-col :span="12">
                  <UploadFile style="width: 200px;"
                    :data="{ img_url: form.company_license_url, image_show_url: image_license_url }"
                    @success="handleUploadSuccess" />
                </el-col>

              </el-form-item>

              <el-row :gutter="20">
                <el-col :span="12" :offset="6" class="text-center">
                  <el-button @click="cancelClick">返回</el-button>
                  <el-button type="primary" @click="confirmClick">提交</el-button>

                </el-col>
              </el-row>


            </el-form>
          </div>
        </el-card>

      </el-col>
    </el-row>
    <!-- 注册成功弹窗 -->
    <el-dialog v-model="dialogVisibleAuth" title="提交认证信息成功" width="50%" :before-close="handleClose" center>
      <el-row>
        <el-col :sm="24" :lg="24">
          <el-result icon="success" title="提交认证信息成功！请等待管理员进行审核，点击确定按钮您将以未认证的方式登录系统">
            <template #extra>
              <el-button type="primary" @click="gotoHome">确定</el-button>
            </template>
          </el-result>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script setup>
// vue 内部方法
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
// 页面组件
import UploadFile from "~/components/UploadFile.vue"

// 公共方法
import { useInitTable, useInitForm } from "~/composables/useCommon"
import { toast } from '~/composables/util'

// api
import {
  putUserWithCompany,
  postUser,
} from "~/api/user/index"


const store = useStore()
const router = useRouter()

// 手机号验证
const checkPhone = (rule, value, callback) => {
  // 手机号验证
  if (!value) {
    return callback(new Error("手机号不能为空"));
  } else {
    const reg = /^1[3456789]\d{9}$/;
    if (reg.test(value)) {
      callback();
    } else {
      return callback(new Error("请输入正确的手机号"));
    }
  }
}

// 身份证验证
const isCardId = (rule, value, callback) => {
  if (value) {
    const reg =
      /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/;
    const card = reg.test(value);
    if (!card) {
      callback(new Error("身份证号格式有误！"));
    } else {
      callback();
    }
  } else {
    callback();
  }
  callback();
};


// 初始化表单
const {
  formDrawerRef,
  formRef,
  form,
  rules,
  editId,
  drawerTitle,
  handleSubmit,
  resetForm,
  handleCreate,
  handleEdit,
} = useInitForm({
  create: postUser,
  update: putUserWithCompany,
  form: {
    mobile: "",
    id_card: "",
    id_card_front_url: '',
    id_card_front_image_id: '',
    id_card_back_url: '',
    id_card_back_image_id: '',
    nickname: "",
    first_name: "",
    company_mobile: '',
    company_license_number: '',
    company_corporation_front_url: '',
    company_corporation_front_image_id: '',
    company_corporation_back_url: '',
    company_corporation_back_image_id: '',
    company_id_card: '',
    company_corporation: '',
    company_license_url: '',
    company_image_id: '',
    is_auth: '3',
  },
  // onGetDataSuccess: (res) => {
  //   // 将接口返回的公司列表数据命名为companyAllList
  //   companyAllList.value = res.data.name;
  // },
  rules: {
    first_name: [
      {
        required: true,
        message: "姓名不能为空",
        trigger: "blur",
      },
    ],

    mobile: [
      { required: true, message: "请输入正确手机号", trigger: "blur" },
      { validator: checkPhone, trigger: ["blur", "change"] },
    ],
    id_card: [
      { required: true, message: "请输入正确身份证号", trigger: "blur" },
      { validator: isCardId, trigger: ["blur", "change"] },
    ],
  }

});


// 提交表单
const confirmClick = () => {
  formRef.value.validate((valid) => {
    if (!valid) return;

    form.username = store.state.user.username

    form.company_id = store.state.user.company_id

    form.user_id = store.state.user.id
    if (store.state.user.is_auth == '1') {
      form.is_auth = 1
    } else {
      form.is_auth = 3
    }
    putUserWithCompany(form).then((res) => {
    })
      .finally(() => {
        // 跳转用户中心
        dialogVisibleAuth.value = true
      });
  })
}

// 提交认证信息弹窗
const dialogVisibleAuth = ref(false)
const gotoHome = () => {
  dialogVisibleAuth.value = false
  // 直接请求用户数据
  store.dispatch("getUserInfo").then().finally(() => {
    router.replace({ path: '/' })
  })


}


// 营业执照上传成功
const image_license_url = ref('')
const handleUploadSuccess = (res) => {
  form.company_license_url = res.response.url;
  image_license_url.value = res.response.url;
  form.company_image_id = res.response.id;
}


const image_id_card_front_url = ref('')
const image_id_card_back_url = ref('')
const image_company_corporation_front_url = ref('')
const image_company_corporation_back_url = ref('')
// 身份证正面上传成功
const handleCardFrontUploadSuccess = (res) => {
  form.id_card_front_url = res.response.url;
  form.id_card_front_image_id = res.response.id;
  image_id_card_front_url.value = res.response.http_url;
}

// 身份证反面上传成功
const handleCardBackUploadSuccess = (res) => {
  form.id_card_back_url = res.response.url;
  form.id_card_back_image_id = res.response.id;
  image_id_card_back_url.value = res.response.http_url;
}


// 法人正反面身份证
const handleCorporationCardBackUploadSuccess = (res) => {
  form.company_corporation_back_url = res.response.url;
  form.company_corporation_back_image_id = res.response.id;
  image_company_corporation_back_url.value = res.response.http_url;
}

const handleCorporationCardFrontUploadSuccess = (res) => {
  form.company_corporation_front_url = res.response.url;
  form.company_corporation_front_image_id = res.response.id;
  image_company_corporation_front_url.value = res.response.http_url;
}




</script>

<style scoped>
.content {
  width: 100%;
  height: 100vh;
  overflow-y: scroll;
  padding: 30px;
  background-color: rgb(190, 188, 185);
}

.width-100 {
  width: 100%;
}

.border-radius-10 {
  border-radius: 10px;
}

.height-70 {
  height: 70%;
}
</style>
